package me.jzn.alib.utils;

import android.app.Activity;
import android.graphics.Paint;
import android.text.InputFilter;
import android.text.method.LinkMovementMethod;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.CycleInterpolator;
import android.view.animation.TranslateAnimation;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.IdRes;

import me.jzn.core.utils.ArrayUtil;

public class UIUtil {
    public static void enableScreenShot(Activity activity, boolean enable) {
        Window window = activity.getWindow();
        final int flag = WindowManager.LayoutParams.FLAG_SECURE;
        if (enable) {
            window.clearFlags(flag);
        } else {
            window.addFlags(flag);
        }
    }

    public static void hideSoftKeyboard(Activity activity) {
        View focusView = activity.getCurrentFocus();
        if (focusView != null) {
            InputMethodManager inputManager = CtxUtil.getSystemService(InputMethodManager.class);
            inputManager.hideSoftInputFromWindow(focusView.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    public static void hideSoftKeyboard(View focusView) {
        if (focusView != null) {
            InputMethodManager inputManager = CtxUtil.getSystemService(InputMethodManager.class);
            inputManager.hideSoftInputFromWindow(focusView.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    public static ViewGroup getViewParent(View view) {
        ViewParent vp = view.getParent();
        if (vp == null) {
            return null;
        }
        if (!(vp instanceof ViewGroup)) {
            return null;
        }
        return (ViewGroup) vp;
    }

    public static void removeView(View view) {
        ViewGroup parent = getViewParent(view);
        if (parent != null) {
            parent.removeView(view);
        }
    }

    public static void clearErrors(EditText... inputs) {
        if (inputs == null) {
            return;
        }
        for (EditText input : inputs) {
            input.setError(null);
        }
    }

    //事实上，无论时inputtype=textCapCharacters,还是android:textAllCaps="true"，代码中设定setTextAllCaps都没用.
    public static void makeUpperCase(EditText et) {
        InputFilter[] fillters = et.getFilters();
        if (fillters == null) {
            fillters = new InputFilter[]{new InputFilter.AllCaps()};
        } else {
            fillters = ArrayUtil.addToTail(fillters, new InputFilter.AllCaps());
        }
        et.setFilters(fillters);
    }

    //中划线
    public static void makeMidLine(TextView tv) {
        tv.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
    }

    public static void makeHref(OnClickListener listener, TextView... tvs) {
        for (TextView tv : tvs) {
            tv.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
            tv.setOnClickListener(listener);
        }
    }

    /**
     * 下划线
     * 参见 makeHref(OnClickListener listener, TextView... tv)
     */
    @Deprecated
    public static void makeUnderline(TextView tv) {
        tv.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
    }

    public static void makeScroll(TextView tv) {
        tv.setMovementMethod(ScrollingMovementMethod.getInstance());
    }

    /**
     * 能够识别<a href>xx</a>这样的
     * 改用 enableHref(TextView tv) {
     */
    @Deprecated
    public static void makeHref(TextView tv) {
        tv.setMovementMethod(LinkMovementMethod.getInstance());
    }

    public static void enableHref(TextView tv) {
        tv.setMovementMethod(LinkMovementMethod.getInstance());
    }

    /**
     * 如果填写错误，那么就晃动3下，引起用户注意
     *
     * @param view
     */
    public static void shake(View view) {
        int shakeCnt = 3;
        Animation anim = new TranslateAnimation(0, 10, 0, 0);
        anim.setInterpolator(new CycleInterpolator(shakeCnt));
        anim.setDuration(500);
        view.startAnimation(anim);
    }

    @Deprecated
    public static void removeThisAsOnlickListener(View view, @IdRes int... resids) {
        if (view instanceof OnClickListener) {
            for (int id : resids) {
                view.findViewById(id).setOnClickListener(null);
            }
        }
    }
}
